// 会员服务 - 管理会员类型、权限和购买逻辑

// 会员类型定义
export const MEMBERSHIP_TYPES = {
  NORMAL: 'normal',
  VIP: 'vip',
  SUPER_VIP: 'super_vip'
};

// 会员权限定义
export const MEMBERSHIP_PERMISSIONS = {
  // 普通用户权限
  [MEMBERSHIP_TYPES.NORMAL]: {
    features: [
      '查看摄影师基本信息',
      '浏览部分作品',
      '获取有限的联系机会',
      '基础搜索功能'
    ],
    limits: {
      maxViewWorks: 3,
      maxContactsPerMonth: 1,
      hasWatermark: true,
      hasAds: true
    }
  },
  // VIP用户权限
  [MEMBERSHIP_TYPES.VIP]: {
    features: [
      '查看摄影师全部信息',
      '浏览所有作品',
      '每月5次联系机会',
      '高级搜索功能',
      '作品无水印查看',
      '无广告体验',
      '专属客服支持'
    ],
    limits: {
      maxViewWorks: 100,
      maxContactsPerMonth: 5,
      hasWatermark: false,
      hasAds: false
    }
  },
  // 超级VIP用户权限
  [MEMBERSHIP_TYPES.SUPER_VIP]: {
    features: [
      '查看摄影师全部信息',
      '浏览所有作品并下载原图',
      '无限次联系摄影师',
      '专属摄影师推荐',
      '优先预约权',
      '作品无水印查看和下载',
      '无广告体验',
      '7×24小时专属客服',
      '免费摄影技巧课程',
      '会员专属活动邀请'
    ],
    limits: {
      maxViewWorks: 9999,
      maxContactsPerMonth: 999,
      hasWatermark: false,
      hasAds: false,
      canDownloadOriginals: true
    }
  }
};

// 会员套餐定义
export const MEMBERSHIP_PLANS = {
  [MEMBERSHIP_TYPES.VIP]: [
    {
      id: 'vip-monthly',
      duration: 'month',
      durationText: '月',
      price: 108,
      originalPrice: 128,
      discountText: '首月立减20元！',
      period: 1
    },
    {
      id: 'vip-quarterly',
      duration: 'quarter',
      durationText: '季度',
      price: 158,
      originalPrice: 384,
      discountText: '相当于每月仅需52.7元！',
      period: 3
    },
    {
      id: 'vip-yearly',
      duration: 'year',
      durationText: '年',
      price: 349,
      originalPrice: 1536,
      discountText: '相当于每月仅需29.1元！',
      period: 12
    }
  ],
  [MEMBERSHIP_TYPES.SUPER_VIP]: [
    {
      id: 'super-vip-monthly',
      duration: 'month',
      durationText: '月',
      price: 198,
      originalPrice: 268,
      discountText: '首月立减70元！',
      period: 1
    },
    {
      id: 'super-vip-quarterly',
      duration: 'quarter',
      durationText: '季度',
      price: 398,
      originalPrice: 804,
      discountText: '相当于每月仅需132.7元！',
      period: 3
    },
    {
      id: 'super-vip-yearly',
      duration: 'year',
      durationText: '年',
      price: 899,
      originalPrice: 3216,
      discountText: '相当于每月仅需74.9元！',
      period: 12
    }
  ]
};

// 新人特惠
export const NEW_USER_SPECIAL_OFFER = {
  vipYearly: {
    price: 299,
    originalPrice: 1536,
    discountText: '新人专享！原价1536元，仅需299元！',
    saving: 1237,
    savingPercent: 80
  },
  superVipYearly: {
    price: 799,
    originalPrice: 3216,
    discountText: '新人专享！原价3216元，仅需799元！',
    saving: 2417,
    savingPercent: 75
  }
};

// 检查用户是否有某项权限
export function hasPermission(user, permission) {
  if (!user || !user.membershipType) {
    user = { membershipType: MEMBERSHIP_TYPES.NORMAL };
  }
  
  const permissions = MEMBERSHIP_PERMISSIONS[user.membershipType] || 
                     MEMBERSHIP_PERMISSIONS[MEMBERSHIP_TYPES.NORMAL];
  
  return permissions.features.includes(permission);
}

// 获取用户权限信息
export function getUserPermissions(user) {
  if (!user || !user.membershipType) {
    return MEMBERSHIP_PERMISSIONS[MEMBERSHIP_TYPES.NORMAL];
  }
  
  return MEMBERSHIP_PERMISSIONS[user.membershipType] || 
         MEMBERSHIP_PERMISSIONS[MEMBERSHIP_TYPES.NORMAL];
}

// 计算会员到期时间
export function calculateExpiryDate(startDate, period) {
  const expiry = new Date(startDate);
  expiry.setMonth(expiry.getMonth() + period);
  return expiry;
}

// 格式化会员到期时间显示
export function formatExpiryDate(date) {
  if (!date) return '无';
  const d = new Date(date);
  return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
}

// 获取会员类型显示名称
export function getMembershipDisplayName(type) {
  const names = {
    [MEMBERSHIP_TYPES.NORMAL]: '普通用户',
    [MEMBERSHIP_TYPES.VIP]: 'VIP会员',
    [MEMBERSHIP_TYPES.SUPER_VIP]: '超级VIP会员'
  };
  return names[type] || '普通用户';
}

// 获取会员类型（与getMembershipDisplayName保持兼容）
export function getMembershipType(type) {
  const names = {
    [MEMBERSHIP_TYPES.NORMAL]: 'NORMAL',
    [MEMBERSHIP_TYPES.VIP]: 'VIP',
    [MEMBERSHIP_TYPES.SUPER_VIP]: 'SUPER_VIP'
  };
  return names[type] || 'NORMAL';
}

// 检查权限
// 直接使用type进行权限检查
export function checkPermission(type, permission) {
  // 映射权限字符串到对应的功能描述
  const permissionMap = {
    'VIEW_FULL_PROFILE': '查看摄影师全部信息',
    'VIEW_ALL_WORKS': '浏览所有作品',
    'CONTACT_PHOTOGRAPHER': '每月5次联系机会',
    'DOWNLOAD_ORIGINAL': '浏览所有作品并下载原图'
  };
  
  // 获取实际的功能描述
  const actualPermission = permissionMap[permission] || permission;
  
  const permissions = MEMBERSHIP_PERMISSIONS[type] || 
                     MEMBERSHIP_PERMISSIONS[MEMBERSHIP_TYPES.NORMAL];
  
  return permissions.features.includes(actualPermission);
}

// 检查是否有多项访问权限
export function hasAccess(type, permissions) {
  if (!Array.isArray(permissions)) {
    permissions = [permissions];
  }
  
  return permissions.every(permission => checkPermission(type, permission));
}

// 获取剩余联系次数
export function getRemainingContactCount(type, usedCount = 0) {
  const permissions = MEMBERSHIP_PERMISSIONS[type] || 
                     MEMBERSHIP_PERMISSIONS[MEMBERSHIP_TYPES.NORMAL];
  
  const maxContacts = permissions.limits.maxContactsPerMonth;
  return maxContacts - usedCount;
}